<?php
namespace app\admin\controller;
use app\admin\model\Citys;
use app\admin\model\IndexMenuList;
use app\admin\model\IndexRotationPic;
use think\Request;

class IndexRotation extends Base
{
    protected function _initialize(){
        $this->checkLogin();
        $this->menuLoad();
    }
    public function index(){
        return $this->fetch('public/help');
    }

    public function rotationList(){

        $indexRotationPic = new IndexRotationPic();
        $where['id'] = ['neq','0'];
        $rs = $indexRotationPic->getAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function addIndexRotationPic(Request $request){
        if($request->post()){
            $data = $request->post();
            if($data['name'] == '' || $data['sort'] == '' || $data['img'] == '' || $data['position'] == '')return $this->error('参数不完整');
            $indexRotationPic = new IndexRotationPic();
            $rs = $indexRotationPic->add($data);
            if(!$rs)return $this->error('添加失败');
            return $this->success('添加成功');
        }
        return $this->fetch();
    }

    public function editIndexRotationPic(Request $request){
        $indexRotationPic = new IndexRotationPic();
        if($request->post()){
            $data = $request->post();
            if($data['name'] == '' || $data['sort'] == '' || $data['img'] == '' || $data['position'] == '')return $this->error('参数不完整');
            $where['id'] = $data['id'];
            unset($data['id']);
            $rs = $indexRotationPic->edit($where,$data);
            if(!$rs)return $this->error('修改失败');
            return $this->success('修改成功','admin/IndexRotation/rotationList');
        }

        $rs = $indexRotationPic->getOne($request->get());
        if(!empty($rs)){
            $this->assign('rs',$rs);
        }else{
            return $this->error('未查询到此数据');
        }
        return $this->fetch();
    }

    public function delIndexRotationPic(Request $request){
        $indexRotationPic = new IndexRotationPic();
        $data = $request->get();
        $rs = $indexRotationPic->del($data);
        if($rs)return $this->success('删除成功');
        return $this->error('删除失败');
    }

    public function indexMenuList(){
        $indexMenuList = new IndexMenuList();
        $where['id'] = ['gt',0];
        $rs = $indexMenuList->getAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function addIndexMenuList(Request $request){
        if($request->post()){
            $data = $request->post();
            if($data['name'] == '' || $data['sort'] == '' || $data['url'] == '' )return $this->error('参数不完整');
            $indexMenuList = new IndexMenuList();
            $rs = $indexMenuList->add($data);
            if(!$rs)return $this->error('添加失败');
            return $this->success('添加成功');
        }
        return $this->fetch();
    }

    public function editIndexMenuList(Request $request){
        $indexMenuList = new IndexMenuList();
        if($request->post()){
            $data = $request->post();
            if($data['name'] == '' || $data['sort'] == '' || $data['url'] == '' )return $this->error('参数不完整');
            $where['id'] = $data['id'];
            unset($data['id']);
            $rs = $indexMenuList->edit($where,$data);
            if(!$rs)return $this->error('修改失败');
            return $this->success('修改成功','admin/IndexRotation/indexMenuList');
        }
        $rs = $indexMenuList->getOne($request->get());
        if(!empty($rs)){
            $this->assign('rs',$rs);
        }else{
            return $this->error('未查询到此数据');
        }
        return $this->fetch();
    }

    public function delIndexMenuList(Request $request){
        $indexMenuList = new IndexMenuList();
        $data = $request->get();
        $rs = $indexMenuList->del($data);
        if($rs)return $this->success('删除成功');
        return $this->error('删除失败');
    }

    public function citysList(Request $request){
        $pid = $request->get('pid');
        if($pid == null){
            $pid = 0;
        }
        $city = new Citys();
        $where['pid'] = $pid;
        $rs = $city->getAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }


    public function indexSelected(){
        return $this->fetch('public/help');
    }
}